﻿///***************************************************************
/// 시  스  템 : Updms.Etc
/// 단위시스템 : ETC
/// 계      층 : Presentation Layer
/// 프로그램명 : Etc1013m
/// 파  일  명 : Etc1013m.aspx.cs
/// 설      명 : 구글캘린더
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-11-24 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;
using Updms.Etc.Exe;


namespace Updms.Etc
{
	public partial class Etc1013m : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Etc1013m";
		public static readonly string gs_path_info = "/updms/Etc/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Etc etc = new CommCode.Etc(LANG_SET);

		//검색
		protected string kw1 = "";	//기준년월
		protected string kw2 = "";	//범주
		protected string kw3 = "";	//제목
		protected string kw4 = "";	//장소
		protected string kw5 = "";	//내용

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
			SetClientScriptNoPostBack();
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetLayout();
			SetBaseYear();
			SetCategory();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion		

		#region SetClientScript, SetClientScriptNoPostBack

		private void SetClientScript()
		{
			//엔터검색
			txt_search_title.Attributes.Add("onkeypress", "enterSearch(event)");
			txt_search_location.Attributes.Add("onkeypress", "enterSearch(event)");
			txt_search_contents.Attributes.Add("onkeypress", "enterSearch(event)");
		}

		//포스트백과 무관하게 이벤트 지정
		private void SetClientScriptNoPostBack()
		{
			//계정
			cbl_search_category.Items[0].Attributes.Add("onclick", "checkBoxListCheck('cbl_search_category');");
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";			

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_base_year.SelectedValue = kw1.Substring(0, 4).ToString();
						ddl_search_base_month.SelectedValue = kw1.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = WebControlUtil.GetCheckBoxListValue(cbl_search_category, 1);
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						WebControlUtil.SetCheckBoxListValue(cbl_search_category, kw2);
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))
					{
						kw3 = txt_search_title.Text.Trim();
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						txt_search_title.Text = kw3;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))
					{
						kw4 = txt_search_location.Text.Trim();
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						txt_search_location.Text = kw4;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw5"]))
					{
						kw5 = txt_search_contents.Text.Trim();
					}
					else
					{
						kw5 = Server.UrlDecode(Request.QueryString["kw5"]);
						txt_search_contents.Text = kw5;
					}

					break;
				case "SEARCH":
					kw1 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					kw2 = WebControlUtil.GetCheckBoxListValue(cbl_search_category, 1);
					kw3 = txt_search_title.Text.Trim();
					kw4 = txt_search_location.Text.Trim();
					kw5 = txt_search_contents.Text.Trim();
					break;
			}
		}
		
		#endregion

		#region 마스터 Method (SetLayout, SetBaseYear, SetCategory)

		//레이아웃
		private void SetLayout()
		{
			etc.SetLayout(rbl_layout);
		}

		//기준년도
		private void SetBaseYear()
		{
			ddl_search_base_year.Items.Clear();

			int li_f_year = 2010;	//2010년부터 조회
			int li_t_year = Convert.ToInt32(DateUtil.GetYearEx(1, "yyyy"));
			for (int i = li_f_year; i <= li_t_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			ddl_search_base_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 기본값은 현재년월
			ddl_search_base_month.SelectedValue = DateUtil.GetMonthEx(0, "MM");
		}

		//범주
		private void SetCategory()
		{
			etc.SetCategory(cbl_search_category);
			cbl_search_category.Items.Insert(0, new ListItem(GetXmlMsg("STR_019"), "ALL"));	//전체선택
		}

		#endregion

		#region 디테일 월별 (SetPrevNextMonth, SetEnabledMonthBtn, SetThisMonth)

		//이전달, 다음달버튼 누르면
		private void SetPrevNextMonth(int pi_month_cnt)
		{
			string ls_ym = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
			string ls_calc_ym = DateUtil.GetCalcMonth(ls_ym, pi_month_cnt, "yyyy-MM");

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_ym.Substring(0, 4).ToString()))
			{
				ddl_search_base_year.SelectedValue = ls_calc_ym.Substring(0, 4).ToString();
				ddl_search_base_month.SelectedValue = ls_calc_ym.Substring(5, 2).ToString();
			}

			//버튼처리
			SetEnabledMonthBtn();
		}

		//이전달, 다음달버튼 처리
		private void SetEnabledMonthBtn()
		{
			ibtn_prev_month.Enabled
				= (ddl_search_base_year.SelectedIndex == 0 && ddl_search_base_month.SelectedIndex == 0) ? false : true;

			ibtn_next_month.Enabled
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1 && ddl_search_base_month.SelectedIndex == 11) ? false : true;
		}

		//당월설정
		private void SetThisMonth()
		{
			string ls_this_ym = DateUtil.GetNow("yyyy-MM");
			ddl_search_base_year.SelectedValue = ls_this_ym.Substring(0, 4).ToString();
			ddl_search_base_month.SelectedValue = ls_this_ym.Substring(5, 2).ToString();
			SetEnabledMonthBtn();
		}

		#endregion

		#region 디테일 년도별 (SetPrevNextYear, SetEnabledYearBtn, SetThisYear)

		//이전년, 다음년버튼 누르면
		private void SetPrevNextYear(int pi_year_cnt)
		{
			int li_calc_year = Convert.ToInt32(ddl_search_base_year.SelectedValue) + pi_year_cnt;	//2008 + (-1)

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, li_calc_year.ToString()))
			{
				ddl_search_base_year.SelectedValue = li_calc_year.ToString();
			}

			//버튼처리
			SetEnabledYearBtn();
		}

		//이전년, 다음년버튼 처리
		private void SetEnabledYearBtn()
		{
			ibtn_prev_year.Enabled
				= (ddl_search_base_year.SelectedIndex == 0) ? false : true;

			ibtn_next_year.Enabled
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1) ? false : true;
		}

		//당년설정
		private void SetThisYear()
		{
			string ls_this_year = DateUtil.GetNow("yyyy");
			ddl_search_base_year.SelectedValue = ls_this_year;
			SetEnabledYearBtn();
		}

		#endregion

		#region CRUD (SearchProcess)

		//검색
		private void SearchProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();
			
			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_category", typeof(string), kw2);
			lo_dp.AddProperty("ls_title", typeof(string), kw3);
			lo_dp.AddProperty("ls_location", typeof(string), kw4);
			lo_dp.AddProperty("ls_contents", typeof(string), kw5);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Etc1013m_NT _Etc1013m_NT = new Etc1013m_NT())
			{
				if (rbl_layout.SelectedValue == "AH001")
				{
					lo_ds.Merge(_Etc1013m_NT.SelectMonthly(lo_dp));
				}
				else
				{
					lo_ds.Merge(_Etc1013m_NT.SelectYearly(lo_dp));
				}
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();

			string search_condition = "?sm=" + gs_sm + "&kw1=" + Server.UrlEncode(kw1) + "&kw2=" + Server.UrlEncode(kw2) + "&kw3=" + Server.UrlEncode(kw3) + "&kw4=" + Server.UrlEncode(kw4) + "&kw5=" + Server.UrlEncode(kw5);

			//즐겨찾기
			ibtn_favorite.Attributes.Add("onclick", "javascript:goFavoriteSearchPage('" + PID + "','" + search_condition + "');return false;");

			switch (rbl_layout.SelectedValue)
			{
				case "AH001":	//월별
					tabel_btn_month.Visible = true;
					tabel_btn_year.Visible = false;
					lbl_this_month.Text = DateUtil.ConvertDateMask(kw1, "yyyy-MM", LANG_SET);
					SetEnabledMonthBtn();
					break;
				case "AH002":	//년도별
					tabel_btn_month.Visible = false;
					tabel_btn_year.Visible = true;
					lbl_this_year.Text = DateUtil.ConvertDateMask(kw1, "yyyy", LANG_SET);
					SetEnabledYearBtn();
					break;
				default:
					tabel_btn_month.Visible = true;
					tabel_btn_year.Visible = false;
					lbl_this_month.Text = DateUtil.ConvertDateMask(kw1, "yyyy-MM", LANG_SET);
					SetEnabledMonthBtn();
					break;
			}
		}

		#endregion

		#region Page_PreRender

		//Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					bool lb_access = access.CheckPgmUsingGrant(USER_DIV, PID);
					PagePreRender.SetVisibleCtrl(this, lb_access, access.GetVisibleCtrl(PID));
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridView gv = (GridView)sender;

			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//연도별로 보는경우 (홀짝수)월별로 셀 색깔을 다르게 함
				for (int i = 0; i < gv.Columns.Count; i++)
				{
					string cell_data = e.Row.Cells[i].Text;
					string tmp = ConversionUtil.ExtractStrBtwnChar(cell_data, "dt_google\">", "</span><br />");
					string[] arr_month;
					int month = 1;

					if (tmp.Contains("/"))
					{
						arr_month = tmp.Split('/');
						if (arr_month.Length > 1)
						{
							month = Convert.ToInt32(arr_month[0].ToString());
						}

						//짝수월이면
						string td_class = ((month % 2) == 0) ? "v_align_t bg_bluesky" : "v_align_t bg_white";
						e.Row.Cells[i].Attributes.Add("class", td_class);
					}
					else
					{
						e.Row.Cells[i].Attributes.Add("class", "v_align_t");
					}
				}
			}
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//이전달
		protected void ibtn_prev_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(-1);
			SearchProcess();
		}

		//다음달
		protected void ibtn_next_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(1);
			SearchProcess();
		}

		//당월
		protected void ibtn_this_month_Click(object sender, EventArgs e)
		{
			SetThisMonth();
			SearchProcess();
		}

		//이전년
		protected void ibtn_prev_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(-1);
			SearchProcess();
		}

		//다음년
		protected void ibtn_next_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(1);
			SearchProcess();
		}		
		
		//당년
		protected void ibtn_this_year_Click(object sender, EventArgs e)
		{
			SetThisYear();
			SearchProcess();
		}

		//상세일정 보기
		protected void lbtn_view_detail_Click(object sender, EventArgs e)
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("li_seq", typeof(int), Convert.ToInt32(h_seq.Value));
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);
			using (Etc1013m_NT _Etc1013m_NT = new Etc1013m_NT())
			{
				lo_ds.Merge(_Etc1013m_NT.Select(lo_dp));
			}

			if (DataSetUtil.CheckRowCount(lo_ds, "UPDMS_ETC_PLANNER"))
			{
				//콘트롤에 바인딩
				lbl_pk.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_ETC_PLANNER", "Seq");
				lbl_st_dt.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_ETC_PLANNER", "St_Dt");
				lbl_ed_dt.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_ETC_PLANNER", "Ed_Dt");
				lbl_category.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_ETC_PLANNER", "Category_Nm");
				ltr_title.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_ETC_PLANNER", "Title");
				lbl_contents.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_ETC_PLANNER", "Contents");
				lbl_location.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_ETC_PLANNER", "Location");

				mpe_view_detail.Show();
			}
		}

		#endregion		

	}
}
